/** uupaa-suketrans.js
 *
 * uupaa-suketrans.js is Alpha png images transparent for IE6
 *  - uupaa.js spin-off project
 *
 * @author Takao Obara <com.gmail@js.uupaa>
 * @license uupaa-suketrans.js is licensed under the terms and conditions of the MIT licence.
 * @version 1.0
 * @date 2008-08-07
 * @see <a href="http://code.google.com/p/uupaa-js/">uupaa.js Home(Google Code)</a>
 * @see <a href="http://code.google.com/p/uupaa-js-spinoff/">uupaa.js SpinOff Project Home(Google Code)</a>
 */
function suketrans() {
  var sheet = document.createStyleSheet(),
      behavior = "behavior: expression(suketrans._expression(this))";
  sheet.addRule("img",    behavior);
  sheet.addRule("input",  behavior);
  sheet.addRule(".png",   behavior);
  sheet.addRule(".alpha", behavior);
};

suketrans.already = false;

(function() {

var path = "http://uupaa-js-spinoff.googlecode.com/svn/trunk/uupaa-suketrans.js/",
    spacer = "b32.png";

suketrans._expression = function(elm) {
  var reg = RegExp(spacer), run = 0;

  switch (elm.tagName.toLowerCase()) {
  case "img": // <img>
    if (/.png$/i.test(elm.src)) {
      if (reg.test(elm.src)) {
        break; // nop
      }
      elm.suketrans_org_src = elm.src;

      // apply filter
      var w = elm.width,
          h = elm.height;
      suketrans._setAlphaLoader(elm, elm.src, "image");
      elm.src = path + spacer;
      elm.width = w; // hasLayout = true
      elm.height = h;

      ++run;
    } else {
      // <img src="*.png">  ->  <img src="*.gif">
      if (!reg.test(elm.src)) {
        elm.suketrans_org_src = elm.src;

        // disable filter and make it original size
        suketrans._unsetAlphaLoader(elm);
        elm.style.width = "auto";
        elm.style.height = "auto";
      }
    }
    break;

  case "input": // <input type="image">
    if (elm.type !== "image") {
      break;
    }
    if (/.png$/i.test(elm.src)) {
      if (reg.test(elm.src)) {
        break; // nop
      }
      elm.suketrans_org_src = elm.src;

      // apply filter
      suketrans._setAlphaLoader(elm, elm.src, "image");
      elm.src = path + spacer;
      elm.style.zoom = 1; // hasLayout = true

      ++run;
    } else {
      // <input type="image" src="*.png">  ->  <input type="image" src="*.gif">
      if (!reg.test(elm.src)) {
        elm.suketrans_org_src = elm.src;

        // disable filter
        suketrans._unsetAlphaLoader(elm);
        elm.style.width = "auto";
        elm.style.height = "auto";
      }
    }
    break;

  default: // background-image
    var url = suketrans._getBackgroundImage(elm);
    if (url === "none") {
      suketrans._unsetAlphaLoader(elm);
      break;
    }
    if (reg.test(url)) {
      break; // nop
    }
    if (/.png$/i.test(url)) {

      // apply filter
      suketrans._setAlphaLoader(elm, url, "crop");
      elm.style.backgroundImage = "url(" + path + spacer + ")";

      elm.style.zoom = 1; // hasLayout = true

      ++run;
    } else { // *.png → *.jpg
      suketrans._unsetAlphaLoader(elm);
    }
  }

  if (run) {
    suketrans._bugfix(elm);
  }
  // attach spy and purge behavior
  if (!("suketrans_spy" in elm)) {
    elm.attachEvent("onpropertychange", suketrans._onpropertychange);
    elm.suketrans_spy = 1;
  }
  elm.style.behavior = "none";
};

suketrans._getBackgroundImage = function(elm) {
  var url = elm.style.backgroundImage || elm.currentStyle.backgroundImage;
  if (!url) {
    return "none";
  }
  var m = url.match(/^url\((.*)\)$/);
  if (!m) {
    return "none";
  }
  m[1] = m[1].replace(/["']/g, ""); // "'http://...'" -> "http://"
  return m[1];
};

// spy
suketrans._onpropertychange = function() {
  var evt = window.event;
  switch (evt.propertyName) {
  case "style.backgroundImage":
  case "src":
    suketrans._expression(evt.srcElement);
    break;
  }
};

// set alpha image loader
suketrans._setAlphaLoader = function(elm, src, method) {
  var aloader = "DXImageTransform.Microsoft.AlphaImageLoader";
  if (elm.filters.length && aloader in elm.filters) {
    elm.filters[aloader].enabled = 1;
    elm.filters[aloader].src = src;
  } else {
    elm.style.filter += " progid:" + aloader
                     +  "(src='" + src + "', sizingMethod='" + method + "')";
  }
};

// unset alpha image loader
suketrans._unsetAlphaLoader = function(elm) {
  var aloader = "DXImageTransform.Microsoft.AlphaImageLoader";
  if (elm.filters.length && aloader in elm.filters) {
    elm.filters[aloader].enabled = 0;
  }
};

suketrans._bugfix = function(elm) {
  var i = 0, sz = elm.childNodes.length, c;

  function fix(e) {
    switch (e.tagName.toLowerCase()) {
    case "a":        e.style.cursor = "pointer"; // break through;
    case "input":
    case "select":
    case "textarea": !e.style.position && (e.style.position = "relative");
    }
  }

  fix(elm);
  for (; i < sz; ++i) {
    c = elm.childNodes[i];
    if (c.nodeType === 1) {
      fix(c);
      c.firstChild && suketrans._bugfix(c);
    }
  }
};

if (document.uniqueID) { // IE5.5 and IE6
  var version = parseFloat(navigator.userAgent.match(/MSIE ([\d]\.[\d][\w]?)/)[1]);
  if (version === 5.5 || version === 6) {
    window.attachEvent("onload", function() {
      suketrans();
      suketrans.already = true;
    });
  }
}

})();

